import 'package:appflutterhomeschool/model/get_chapter_model.dart';
import 'package:appflutterhomeschool/res/styles.dart';
import 'package:appflutterhomeschool/ui/dialogs/prompt_dialog_widgets.dart';
import 'package:appflutterhomeschool/ui/widgets/button_widget.dart';
import 'package:appflutterhomeschool/ui/widgets/general_page_jump_widget.dart';
import 'package:appflutterhomeschool/ui/widgets/my_app_bar.dart';
import 'package:appflutterhomeschool/ui/widgets/widgets.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

//获取章节页面

class GetChapterPage extends StatefulWidget {
  final List<ChapteModel> chapteList;
  final String homworkSum;

  GetChapterPage({this.chapteList, this.homworkSum});

  @override
  _GetChapterPageState createState() => _GetChapterPageState();
}

class _GetChapterPageState extends State<GetChapterPage> {
  TextEditingController textEditingController = TextEditingController();
  String name = "未添加";
  List<ChapteModel> chapteList;

  @override
  void initState() {
    super.initState();
    if (widget.homworkSum != null) {
      textEditingController.text = widget.homworkSum;
    }
    chapteList = widget.chapteList;
    if (chapteList != null) {
      if (chapteList.first.name != null) {
        name = "";
        chapteList.forEach((element) {
          name = name + element.order + "\t";
        });
      }
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        appBar: MyAppBar2(title: "添加系统推荐作业"),
        body: Column(
          children: [
            GeneralPageJumpWidget(
              title: "选择作业范围",
              rightContent: name,
              onTap: () {
                Fluttertoast.showToast(msg: "${chapteList.length}");
                PromptDialogWidgets(context).showChapterDialog(chapteList,
                    valueChanged: (List<ChapteModel> chapteModel) {
                  chapteList = chapteModel;
                  if (chapteList.first.name != null) {
                    name = "";
                    chapteList.forEach((element) {
                      name = name + element.order + "\t";
                    });
                  }
                  setState(() {});
                });
              },
            ),
            RightEditWidget(
              visible: true,
              hintText: "请输入题目数量",
              textEditingController: textEditingController,
              rightTitle: "选择题目数量",
              leftTextStyle: TextStyles.textDark37,
            ),
            Spacer(),
            ButtonWidget().rectangleButtonWidget(
                buttonName: "立即生成",
                onPressed: () {
                  if (chapteList.first.name == null) {
                    Fluttertoast.showToast(msg: "请选择章节");
                    return;
                  }

                  if (textEditingController.text == "") {
                    Fluttertoast.showToast(msg: "请输入题的数量");
                    return;
                  }

                  Navigator.pop(context, {
                    "sum": textEditingController.text,
                  });
                }),
          ],
        ),
      ),
      onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
    );
  }
}
